#include "stdafx.h"
#include "snetwork/CDynamicClass.h"
#include "standard/CThrowException.h"
#include "snetwork/Session.h"
#include "standard/nfile.h"
#include "standard/nstring.h"
#include "readfile.h"
#include "processlog.h"

ClassMap theDynamicClassMap;

CDynamicClass::CDynamicClass()
{
    m_getarr.clear();
    m_recvstr.erase();
    m_method.erase();

	m_textinfo["found"] = "1";
}

int CDynamicClass::exec()
{
    return 0;
}

bool CDynamicClass::isFILES()
{
    return (*m_FILES).empty()?false:true;
}

bool CDynamicClass::isGET()
{
    return (*m_GET).empty()?false:true;
}

bool CDynamicClass::isPOST()
{
    return (*m_POST).empty()?false:true;
}

/**
 * sendArr  要发数的数组
 * n_rbArr  要接收的数组
 */ 
int CDynamicClass::execute(void *parr)
{
	try {
	
		paramArr *m_parr;
		vecStr::iterator itor;
		int i=0;
		Sock::Session ses;
		ses.session_start();
        
        m_view_path = get_config("root", "ROOT_PATH") + get_config("path","VIEW_PATH");
		m_parr		= (paramArr *) parr;
		m_sockfd	= m_parr->sockfd;

		m_sendArr	= m_parr->sendArr;
		(*m_sendArr).clear();
		m_parr->textinfo = &m_textinfo;
		m_FILES = m_parr->FILES;
		m_POST = &(m_parr->tPOST);
		m_GET = &(m_parr->tGET);
		m_COOKIE = (mapStrStr*) m_parr->cookie;				

		if (!(*(m_parr->rbArr)).empty()) {
			m_recvstr = (*(m_parr->rbArr))[0];
		}

		for (itor=(*(m_parr->rbArr)).begin(); itor!=(*(m_parr->rbArr)).end(); itor++, i++) {
			getArr(m_getarr[i],*itor);
		}
		this->startview();
        this->exec();
        this->endview();

		(*m_FILES).clear();

    }
    catch (CThrowException e) {
        std::cout<<"error : "<<e.getMessage()<<std::endl;
    }
    
    return 0;
}



int CDynamicClass::startview()
{

    m_ec = new Exec();
#if 0
#if defined(_MSC_VER)    
	m_hDll = LoadLibrary(_T("EXECXML.dll"));
#elif defined(__GNUC__)
    char *error;
    m_hDll = dlopen ("libviewparse.so", RTLD_NOW);     //打开argv[1]指定的动态库 
    if (!m_hDll) {
        fprintf (stderr, "%s\n", dlerror());
        exit(1);
    }
    dlerror();
#endif

	if(m_hDll == NULL) {
		return 0;
	}
	
    typedef Exec* (*PFNCreateExec)();
               
#if defined(_MSC_VER)
	PFNCreateExec pfnFF = (PFNCreateExec)GetProcAddress(m_hDll,"CreateObjectExec");
#elif defined(__GNUC__)	
	PFNCreateExec pfnFF = (PFNCreateExec)dlsym(m_hDll,"CreateObjectExec" );    //指针pf指向test在当前内存中的地址
    if ((error = dlerror()) != NULL) {
        fprintf (stderr, "%s\n", error);
        exit(1);
    }
#endif    
	
	m_ec = (pfnFF)();
#endif    
    return 0;    
}

int CDynamicClass::endview()
{
    delete m_ec;
    m_ec = NULL;
#if 0
    if (m_hDll) {
#if defined(_MSC_VER)	
    FreeLibrary(m_hDll);      //关闭调用动态库句柄
#elif defined(__GNUC__)	
    dlclose(m_hDll);      //关闭调用动态库句柄
#endif	    
    }
#endif    
    return 0;
}

void CDynamicClass::display(std::string tpname)
{
	std::string fileinstr;	
	m_ec->CheckFile( (m_view_path + "/" + tpname + ".htm").c_str() );
	m_ec->Execute( fileinstr );
	(*m_sendArr).push_back(fileinstr);
}
